home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / TODO.PAK / TODOWIN.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  10KB  |  259 lines

  1. //---------------------------------------------------------------------
  2. //
  3. //  TODOWIN.H
  4. //
  5. //      Copyright (c) 1991, 1995 by Borland International
  6. //      All Rights Reserved.
  7. //
  8. //  Defines the following classes, which are useful for building
  9. //  general purpose windows applications:
  10. //
  11. //  WinBase - provides basic data and functionality for
  12. //            windows programming.
  13. //
  14. //  ModalDialog - provides for modal dialog boxes.  Inherits from
  15. //            WinBase, which is a virtual base.
  16. //
  17. //  Window  - provides core functionality for a window under Windows.
  18. //            Inherits from WinBase, which is a virtual base.
  19. //
  20. //---------------------------------------------------------------------
  21. #if !defined(TODOWIN_H)
  22. #define TODOWIN_H
  23.  
  24. #include <winsys/wsysinc.h>
  25. #include <services/except.h>
  26. #include <services/checks.h>
  27. #include <services/cstring.h>
  28.  
  29. class FileError : public xmsg
  30. {
  31.  
  32. public:
  33.  
  34.     FileError( const char *fileName ) :
  35.         xmsg( string("File error in ") + fileName ) {}
  36.  
  37. };
  38.  
  39. //---------------------------------------------------------------------
  40. //
  41. //  class WinBase
  42. //
  43. //      provides the basic data and functionality for all classes
  44. //      used in the windows application.  It is an abstract base class.
  45. //
  46. //---------------------------------------------------------------------
  47.  
  48. class WinBase
  49. {
  50.  
  51. public:
  52.  
  53.     virtual UINT Run() = 0;     // the core function of all windows!  For
  54.                                 // the main application window, this
  55.                                 // provides the message loop.  In modal
  56.                                 // dialogs, it sets up the dialog box,
  57.                                 // calls the dialog proc, and closes down
  58.                                 // the dialog.
  59.  
  60.  
  61.  
  62.     static  HINSTANCE hInst;    // the handle of the current instance
  63.  
  64.     static  HINSTANCE hPrevInst;// the handle of the previous instance
  65.  
  66.     static  LPSTR Cmd;          // pointer to the command line
  67.  
  68.     static  int Show;           // the nCmdShow parameter of the current
  69.                                 // instance
  70.  
  71.     HWND    hWnd();             // access function
  72.  
  73. protected:
  74.  
  75.     HWND hWindow;               // the window handle of the class.  This is
  76.                                 // accessed through hWnd(), and it will
  77.                                 // provide the correct handle for any
  78.                                 // derived class.
  79.  
  80.                                 // NOTE: this field is not initialized by
  81.                                 // the constructor of this class.  It must
  82.                                 // be initialized by the constructor of a
  83.                                 // class derived from this class.
  84.  
  85. };
  86.  
  87. //---------------------------------------------------------------------
  88. //
  89. //  class ModalDialog
  90. //
  91. //      provides basic functionality for modal dialog boxes.  It is an
  92. //      abstract base class.
  93. //
  94. //---------------------------------------------------------------------
  95.  
  96. class ModalDialog : public virtual WinBase
  97. {
  98.  
  99. public:
  100.  
  101.     ModalDialog( HWND );        // constructor.  Since a modal dialog
  102.                                 // needs to know its owner, the handle
  103.                                 // of the owner is passed as a parameter
  104.                                 // to the constructor.
  105.  
  106.     ~ModalDialog();
  107.  
  108.     virtual UINT Run();         // the core of a modal dialog.  It sets
  109.                                 // up the dialog, executes it, and closes
  110.                                 // it down.
  111.  
  112. protected:
  113.  
  114.     virtual BOOL Dispatch( HWND, UINT, WPARAM, LPARAM );
  115.                                 // the core of any window.  Whenever
  116.                                 // DlgProc receives a message it passes
  117.                                 // the message on to Dispatch().  If
  118.                                 // Dispatch() doesn't handle the message
  119.                                 // itself, it should call the Dispatch()
  120.                                 // function in its base class.  Ultimately,
  121.                                 // messages not handled higher up are
  122.                                 // handled by this version of Dispatch(),
  123.                                 // which just returns FALSE, indicating
  124.                                 // that the message wasn't handled by
  125.                                 // the dialog box at all.
  126.  
  127.             UINT Result;        // this holds the value which will be
  128.                                 // returned by Run().  If the dialog box
  129.                                 // needs to return a success code, that
  130.                                 // code should be stored here by the
  131.                                 // dialog handler.
  132.  
  133. private:
  134.  
  135.     virtual LPSTR GetDialogName() = 0;
  136.                                 // returns a far pointer to a string
  137.                                 // that contains the name of the dialog
  138.                                 // resource for the current dialog box.
  139.                                 // This is used in Run() to load the
  140.                                 // resource.
  141.  
  142.     static  BOOL CALLBACK _export DlgProc( HWND, UINT, WPARAM, LPARAM );
  143.                                 // the dialog proc that windows calls
  144.                                 // when it has messages for the current
  145.                                 // dialog.
  146.  
  147.     static  ModalDialog *CurDlg;
  148.                                 // this holds a pointer to the currenly
  149.                                 // active ModalDialog.  It is set up when
  150.                                 // the constructor is called, and is used
  151.                                 // by DlgProc() to route messages to the
  152.                                 // right Dispatch() function.
  153.  
  154. };
  155.  
  156. //---------------------------------------------------------------------
  157. //
  158. //  class Window
  159. //
  160. //      provides the basic functionality for a normal window under
  161. //      windows.
  162. //
  163. //---------------------------------------------------------------------
  164.  
  165. class Window : public virtual WinBase
  166. {
  167.  
  168. public:
  169.  
  170.     virtual BOOL Create();      // creates the window.  This involves
  171.                                 // registering the window class if
  172.                                 // it hasn't already been registered,
  173.                                 // creating the actual window, and
  174.                                 // inserting the window into the internal
  175.                                 // window list.
  176.  
  177.     virtual UINT Run();         // provides the message loop.
  178.  
  179. protected:
  180.  
  181.     virtual LONG Dispatch( UINT, WPARAM, LPARAM );
  182.                                 // dispatches messages in a class derived
  183.                                 // from Window.  When WndProc() receives
  184.                                 // a message, it determines which Window
  185.                                 // object the message should be routed
  186.                                 // to, and calls Dispatch() for that object.
  187.                                 // If the Dispatch() function in a derived
  188.                                 // class does not handle any particular
  189.                                 // message, it should pass that message
  190.                                 // on down to the Dispatch() function in
  191.                                 // its base class.  The version of Dispatch()
  192.                                 // in Window itself just calls DefWindowProc.
  193.  
  194.     virtual BOOL RegisterClass() = 0;
  195.                                 // the derived class should override this
  196.                                 // function and register a window class
  197.                                 // defined appropriately for the program.
  198.  
  199.     virtual BOOL CreateNewWindow() = 0;
  200.                                 // the derived class should override this
  201.                                 // function and create a window that's
  202.                                 // appropriate for the program.
  203.  
  204.     static  LRESULT CALLBACK _export WndProc( HWND, UINT, WPARAM, LPARAM );
  205.                                 // the window proc that windows calls
  206.                                 // when it has messages for any window
  207.                                 // in the current application.  WndProc()
  208.                                 // posts the message to the appropriate
  209.                                 // window.
  210.  
  211.             void Insert();      // should be called from CreateNewWindow()
  212.                                 // after successfully calling the windows
  213.                                 // function CreateWindow().  Insert() adds
  214.                                 // the current object to the Window list,
  215.                                 // enabling normal dispatching of messages
  216.                                 // to the current object.
  217.  
  218. private:
  219.  
  220.     static Window *WinList;     // have to maintain a list of Windows so
  221.     Window *NextWin;            // that we can dispatch messages to the
  222.                                 // correct one.
  223.  
  224.     static Window *InCreate;    // sort-of kludgy.  But there are messages
  225.                                 // that are sent to a window during the
  226.                                 // call to CreateWindow().  We assume that
  227.                                 // any message received by WndProc() during
  228.                                 // a call to CreateWindow() is meant for
  229.                                 // the window being created, and dispatch
  230.                                 // those messages to that window.
  231.  
  232. };
  233.  
  234. inline HWND WinBase::hWnd()
  235. {
  236.     return hWindow;
  237. }
  238.  
  239. inline ModalDialog::ModalDialog( HWND hOwner ) : Result( 0 )
  240. {
  241.     CHECK( CurDlg == 0 );
  242.     CurDlg = this;
  243.     hWindow = hOwner;
  244. }
  245.  
  246. inline ModalDialog::~ModalDialog()
  247. {
  248.     CHECK( CurDlg != 0 );
  249.     CurDlg = 0;
  250. }
  251.  
  252. inline void Window::Insert()
  253. {
  254.     NextWin = WinList;
  255.     WinList = this;
  256. }
  257.  
  258. #endif  // TODOWIN_H
  259.